package com.erhuogo.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.dao.ReportDao;
import com.erhuogo.entity.Report;
import com.erhuogo.enumerate.ReportHandleStatus;
import com.erhuogo.service.ReportService;

/**
 * 举报Service的实现类
 * @author aibinxiao
 * @date 2016年10月26日 下午12:03:14
 */
public class ReportServiceImpl implements ReportService{
	// 日志
	private static Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class);
			
	// 注入OneCategoryDao
	private ReportDao reportDao;

	public ReportDao getReportDao() {
		return reportDao;
	}

	public void setReportDao(ReportDao reportDao) {
		this.reportDao = reportDao;
	}

	// 1.分页条件查询举报信息Service实现类
	@Override
	public GridPageBean<Report> findByPage(String pageNo, String pageSize, String id, String commodityId, String acountId,
			String type, String handleStatus, String startDate, String endDate) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = reportDao.countByParams(id, commodityId, acountId, type, handleStatus, startDate, endDate);
		GridPageBean<Report> result = new GridPageBean<Report>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<Report> rows = reportDao.findPageByParams(id, commodityId, acountId, type, handleStatus, startDate, endDate);
			result.setRows(rows);
		} else{
			List<Report> rows = reportDao.findPageByParams(id, commodityId, acountId, type, handleStatus, startDate, endDate);
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	// 2.更新举报信息操作状态Service实现类
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		// 1表示已处理且举报有效
		if(ReportHandleStatus.REPORTSUCCESS.getStatusCode()==status){
			reportDao.updateStatus(id, ReportHandleStatus.REPORTSUCCESS.getStatusCode());
		}
		// 2表示已处理且举报无效
		if(ReportHandleStatus.REPORTFAILD.getStatusCode()==status){
			reportDao.updateStatus(id, ReportHandleStatus.REPORTFAILD.getStatusCode());
		}
	}
	
}
